﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;


namespace LightControllerDll
{
#if _WIN32
        using ControllerHandleType = Int32;
#else
    using ControllerHandleType = Int64;
#endif

    internal class CstClassLibControllerDll
    {
        const string CommonToolDll = @"./CommonToolDll.dll";
        const string ControllerDll = @"./ExampleDll.dll";
        

        public const int EthernetMode = 0;
        public const int Rs232Mode = 1;
        public const int SUCCESS = 10000;
        public const int ERROR_INIT = 10001;
        public const int ERROR_CLOSE = 10002;
        public const int ERROR_CFG = 10003;
        public const int ERROR_CONNECT = 10004;
        public const int ERROR_RX = 10005;
        public const int ERROR_TX = 10006;
        public const int ERROR_DATA = 10007;
        public const int ERROR_COLLISION = 10008;
        public const int ERROR_IP_ADDRESS = 10009;
        public const int ERROR_SM_ADDRESS = 10010;
        public const int ERROR_GW_ADDRESS = 10011;
        public const int ERROR_GET_ADAPTER = 10012;

        public const int ERROR_GET_DIG_VAl = 10013;
        public const int ERROR_SET_DIG_VAl = 10014;
        public const int ERROR_SET_MUL_DIG_VAl = 10015;


        [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
        public struct Adapter_prm
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 132)]
            public char[] cSn;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
            public char[] cIp;
        }

        [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
        public struct Host_prm
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 21)]
            public char[] cSn;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
            public char[] cIp;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
            public byte[] cMac;
        }

        [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
        public struct Controller_prm
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 21)]
            public char[] cSn;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
            public char[] cIp;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
            public char[] cSm;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
            public char[] cGw;
            public char DHCP;
        }

        public struct MulIntensityValue
        {
            public int channelIndex;
            public int intensity;
        }


        ////////////////////////////////////////////CommonToolDll//////////////////////////////////////////////////////
        [DllImport(CommonToolDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int GetAdapter(ref int AdatterCnt, IntPtr mAdapterPrm);


        [DllImport(CommonToolDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int GetHost(ref int controllerCnt, IntPtr mHostPrm, string AdapterIP);


        [DllImport(CommonToolDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int GetConfigure(byte[] mMAC, IntPtr mConPrm, string AdapterIP);


        [DllImport(CommonToolDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int SetConfigure(byte[] mMAC, ref Controller_prm mConPrm, string AdapterIP);


        [DllImport(CommonToolDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int ConnectIP(string ipAddress, int mTimeOut, ref ControllerHandleType controllerHandle);


        [DllImport(CommonToolDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int DestroyIpConnection(ControllerHandleType controllerHandle);


        [DllImport(CommonToolDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int CreateSerialPort(int serialPortIndex, ref ControllerHandleType controllerHandle);


        [DllImport(CommonToolDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int CreateSerialPort_Baud(int serialPortIndex, int baud, ref ControllerHandleType controllerHandle);


        [DllImport(CommonToolDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int ReleaseSerialPort(ControllerHandleType controllerHandle);

        [DllImport(CommonToolDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int Listen_in_log(int connectType, byte[] m_RecvBuf, int rlen, ControllerHandleType controllerHandle);


        ////////////////////////////////////////////ControllerDll//////////////////////////////////////////////////////
        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int SetTriggerMode(int connectType, int TriggerMode, ControllerHandleType controllerHandle);
        //设置内触发模式

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int GetTriggerMode(int connectType, ref int TriggerMode, ControllerHandleType controllerHandle);


        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int SetExternalTriggerMode(int connectType, int TriggerMode, ControllerHandleType controllerHandle);
        //设置外触发模式

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int GetExternalTriggerMode(int connectType, ref int TriggerMode, ControllerHandleType controllerHandle);

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int SetInternalTriggerInterval(int connectType, int TriggerInterval, ControllerHandleType controllerHandle);

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int GetInternalTriggerInterval(int connectType, ref int TriggerInterval, ControllerHandleType controllerHandle);

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int SetInternalTriggerMode(int connectType, int TriggerMode, int data, ControllerHandleType controllerHandle);
        //设置触发模式，TriggerMode参数为1、2、3。1：单独执行某一流程。2：执行一遍配置中所有流程。3：循环执行所有流程。  data参数为执行哪一个流程，TriggerMode为2和3时data默认为1.

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int SetConstantLightModeData(int connectType, int[] Lightdata, ControllerHandleType controllerHandle);
        //设置常亮数据

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int GetConstantLightModeData(int connectType, int[] Lightdata, ControllerHandleType controllerHandle);

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int SetProgrammableData(int connectType, int step, int[] Lightdata, int[] PulseWidth, ControllerHandleType controllerHandle);
        //设置流程数据

        //[DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        //public static extern
        //int SetConstantLightModeData(int connectType, ref int Lightdata, ControllerHandleType controllerHandle);

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int GetProgrammableData(int connectType, int step, int[] Lightdata, int[] PulseWidth, ControllerHandleType controllerHandle);

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int CleanProgrammableData(int connectType, ControllerHandleType controllerHandle);

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int SetTotalSteps(int connectType, int steps, ControllerHandleType controllerHandle);
        //设置步骤可编程参数

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int GetTotalSteps(int connectType, ref int steps, ControllerHandleType controllerHandle);

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int SetSoftwareTrigger(int connectType, ControllerHandleType controllerHandle);

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int SaveData(int connectType, ControllerHandleType controllerHandle);
        //成功设置保存数据

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int CSTRemind(int connectType, ControllerHandleType controllerHandle);

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int SetDebugMode(int connectType, int TriggerMode, ControllerHandleType controllerHandle);

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int GetDebugMode(int connectType, ref int TriggerMode, ControllerHandleType controllerHandle);

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int SetConstantLightTime(int connectType, int func, int time, ControllerHandleType controllerHandle);

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int GetConstantLightTime(int connectType, ref int time, ControllerHandleType controllerHandle);

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int SetFilteringTime(int connectType, int FilteringTime, ControllerHandleType controllerHandle);
        //设置滤波时间

        [DllImport(ControllerDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int GetFilteringTime(int connectType, ref int FilteringTime, ControllerHandleType controllerHandle);
    }

    internal class RsClassLibControllerDll

    {
        const string RseeController = @"./RseeController.dll";
        [DllImport(RseeController, CallingConvention = CallingConvention.Cdecl)]

        public static extern
        uint RseeController_ConnectNet(string  ulAddress, int iPort);

        [DllImport(RseeController, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        bool RseeController_CloseNet(uint m_DataSocket);

        [DllImport(RseeController, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int RseeController_P_S2_24V_10TCL_TriEn(uint socket, bool onoff);

        [DllImport(RseeController, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int RseeController_P_S2_24V_10TCL_TriMode(uint socket, int mode);

        [DllImport(RseeController, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int RseeController_P_S2_24V_10TCL_ProcessMode(uint socket, int mode);

        [DllImport(RseeController, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int RseeController_P_S2_24V_10TCL_SetRange(uint socket, int[] arr);

        [DllImport(RseeController, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int RseeController_P_S2_24V_10TCL_Onoff(uint socket, bool onoff);

        [DllImport(RseeController, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int RseeController_P_S2_24V_10TCL_LimitTime(uint socket, int time);

        [DllImport(RseeController, CallingConvention = CallingConvention.Cdecl)]
        public static extern
        int RseeController_P_S2_24V_10TCL_ErrReset(uint socket);
    }
}
